// pages/studentInformation/editinformation/index.js
const editlist = {
  id: ""
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList: {

    },
    elementkey: "",
    value: "",
    titleList: [],
    request: {},
    container: '信息提交后不可修改请确认信息无误',
    imgsrc: '../../assets/images/waring.png',
    showModal: false,
    editvalue: '',
    datasetvalue: '',
    id: '',
    titleListto: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id)
    this.setData({
      id: options.id
    })
    this.editdataList();
    // this.gettitleList();
  },
  editdataList () {
    const { id } = this.data
    const request = {
      id: this.data.id
    }
    this.setData({
      appMsg: {
        type: 'loading',
        show: true
      }
    })
    wx.$API['studentInformation/getGraduate'](request).then((response) => {
      this.setData({
        appMsg: {
          type: 'loading',
          show: false
        }
      })


      // console.log(response.data[0])
      this.setData({
        titleListto: response.data.params
      })

      console.log(titleListto, "000000")


      // const data = response.data
      // const dataLength = response.data.length

    }).catch((error) => {
      // 防止直接进入查成绩页面，token无效请求失败

    })
  },

  // gettitleList () {
  //   this.setData({
  //     appMsg: {
  //       type: 'loading',
  //       show: true
  //     }
  //   })

  //   const { } = this.data
  //   const request = {

  //   }

  //   wx.$API['studentInformation/getGraduatePageInfo'](request).then((response) => {
  //     this.setData({
  //       appMsg: {
  //         type: 'loading',
  //         show: false
  //       }
  //     })

  //     this.setData({
  //       titleList: response.data.params
  //     })
  //     this.getoptiondata();


  //   }).catch((error) => {

  //   })
  // },

  // getoptiondata () {
  //   wx.showLoading({
  //     title: '加载中'
  //   })
  //   // for (const key in this.data.tabList) {
  //   //   this.data.titleList.forEach(element => {
  //   //     console.log(element.param, key, 999)

  //   //     if (key == element.param) {
  //   //       console.log(this.data.tabList[key]);
  //   //       element.value = this.data.tabList[key];


  //   //     }
  //   //     else {

  //   //     }

  //   //   });
  //   // }
  //   for (const key in this.data.tabList) {
  //     var arraylist = [];
  //     for (const element of this.data.titleList) {
  //       if (key == element.param) {
  //         console.log(element.param, element.param);
  //         // console.log(this.data.tabList[key]);
  //         arraylist = this.data.tabList[key];
  //         element.value = arraylist;
  //         break
  //       }
  //     }
  //   }

  //   console.log(this.data.titleList)

  //   // this.setData({
  //   //   titleListto: this.data.titleList
  //   // })
  //   setTimeout(() => {
  //     wx.hideLoading();
  //   }, 2500);
  // },

  // editinformation(){

  // },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //获得dialog组件
    this.dialog = this.selectComponent("#dialog");
  },
  showDialog () {
    this.dialog.showDialog();
  },
  confirm () {   //返回
    wx.navigateTo({
      url: '../../studentInformation/graduainforma/index'
    })
  },

  // 失去=焦点事件
  bindblurevent (event) {
    console.log(event.currentTarget.dataset.name, "00000")
    console.log(event.detail.value, 7777)
    if (event.currentTarget.dataset.name != event.detail.value) {

      if (event.currentTarget.dataset.name != 'null' & event.detail.value != '') {
        console.log(event.detail.value, "输入的value值")
        console.log("已修改");
        let editkey = event.currentTarget.dataset.item.param
        let obj = {
          [editkey]: event.detail.value
        }


        this.setData({
          request: {
            ...this.data.request, ...obj
          },
          editvalue: event.detail.value,
          datasetvalue: event.currentTarget.dataset.name
        })
      }
      else {
        this.setData({
          editvalue: event.detail.value,
          datasetvalue: event.currentTarget.dataset.name
        })

      }

      // console.log(this.data.request, "用户已修改");
    }
    else {
      this.setData({
        editvalue: event.detail.value,
        datasetvalue: event.currentTarget.dataset.name
      })
    }

    // if (event.detail.value == event.currentTarget.dataset.item.value) {
    //   console.log("用户未修改");
    // }
    // else {


    // }
    // for (const key in this.data.tabList) {
    //   this.data.titleList.forEach(element => {
    //     console.log(element.param, key, 999)

    //     if (key == element.param) {
    //       console.log(this.data.tabList[key]);
    //       element.value = this.data.tabList[key];


    //     }
    //     else {

    //     }

    //   });
    // }
    // console.log(event.currentTarget.dataset, "999");
    // console.log(event.detail, "0000")
    // this.setData({
    //   // [`tabList.${event.currentTarget.dataset.name}`]: event.detail.value,
    //   ['tabList.' + event.currentTarget.dataset.name]: event.detail.value
    // }, () => {

    // })
    // editlist[event.currentTarget.dataset.name] = event.detail.value;
    // console.log(editlist[event.currentTarget.dataset.name])
    // for (const key in editlist) {
    //   if (editlist[key] === undefined) {
    //     continue
    //   }
    //   if (editlist[key] == this.data.tabList[key]) {
    //     delete editlist[key]

    //   }
    // }

    // this.setData({
    //   editvalue: event.detail,

    // })
    // console.log(this.data.editvalue, 909090909);
  },
  //修改事件
  _cancelEvent () {
    this.dialog.hideDialog();   //显示模态框

    if (this.data.datasetvalue != this.data.editvalue) {
      if (this.data.editvalue != 'null' & this.data.editvalue != '') {
        console.log("已修改");
        this.editinformation();   //调用修改接口提交到后台
      }
      else {

      }


    }
    else {
      console.log("未修改");
      this.noeditinformation();    //调用用户未修改的方法
    }

    for (const key in this.data.request) {
      if (this.data.request[key] === '') {
        delete this.data.request[key]

      }
    }








    // else {
    //   console.log("没有修改")
    //   // this.noeditinformation();    //调用用户未修改的方法
    // }
  },
  noeditinformation () {    //未修改的方法
    editlist.id = this.data.id;
    console.log(editlist.id, "这是id")

    const { id } = this.data
    const request = {
      id: this.data.id
    }
    console.log(request, "测试")
    wx.$API['studentInformation/ChangeGraduate'](request).then((response) => {
      wx.navigateTo({
        url: '../../studentInformation/successfulapplication/index?id=' + editlist.id
      })
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: "none"
      })
    })

  },
  // 修改提交按钮
  editinformation () {
    // request = editlist
    this.data.request.id = this.data.id;
    console.log(this.data.request.id, "这是id")
    let obj = this.data.request;
    wx.$API['studentInformation/ChangeGraduate'](obj).then((response) => {
      console.log(response, "sds")
      wx.navigateTo({
        url: '../../studentInformation/successfulapplication/index?id=' + editlist.id
      })

    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: "none"
      })
    })
  },
  //确认事件
  _confirmEvent () {
    console.log('你点击了取消');
    this.dialog.hideDialog();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})